package com.nativetalk.service.teacher.retrieve;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.email.EmailUtil;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.bean.teacher.TdNtTeacherRetrive;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.*;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by lizhun on 16/1/8.
 */
@Service
public class TeacherRetrieveServiceImpl implements TeacherRetrieveService{
    private static Logger logger = Logger.getLogger(TeacherRetrieveServiceImpl.class);
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Override
    public RetInfo sendEmail(EmailUtil emailUtil) {
        String logInfo = this.getClass().getName() + ":sendEmail:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtTeacherResult tdNtTeacherResult = tdNtTeacherDao.findTdNtTeacherResult(emailUtil.getTea_email());
            if (tdNtTeacherResult == null){

                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TEARCHER.REUSLT.NULL"));

            } else {
                String register = "retrieve" + emailUtil.getTea_email();
                String sencod = "sencod" + emailUtil.getTea_email();
                if (!("0".equals(MemcachedUtils.get(sencod)))) {
                    String code = SmsCodeUtil.getSmsCode();
                    String result = SendMail.sendMail(emailUtil.getTea_email(),code);

                    if (result.equals("0")){
                        retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                        retInfo.setTip(ValidationMessages.getValue("SEND.EMAIL.SUCCESS"));
                        if (MemcachedUtils.get(register) == null){
                            MemcachedUtils.add(register,code,5*60);
                        }else{
                            MemcachedUtils.replace(register,code,5*60);
                        }
                        MemcachedUtils.add(sencod, "0",60);
                        System.out.println("发送邮箱："+MemcachedUtils.get(register));
                    } else {
                        retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                        retInfo.setTip(ValidationMessages.getValue("SEND.EMAIL.ERROR"));
                    }
                } else {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("SEND.EMAIL.OVER"));
                }

            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo retrieve(TdNtTeacherRetrive tdNtTeacherRetrive) {
        String logInfo = this.getClass().getName() + ":retrieve:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtTeacherResult tdNtTeacherResult = tdNtTeacherDao.findTdNtTeacherResult(tdNtTeacherRetrive.getTea_email());
            if (tdNtTeacherResult == null){

                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TEARCHER.REUSLT.NULL"));

            } else {
                String register = "retrieve" + tdNtTeacherRetrive.getTea_email();
                System.out.println("修改密码："+MemcachedUtils.get(register));
                if (tdNtTeacherRetrive.getCode().equals(MemcachedUtils.get(register))){
                    String token = tdNtTeacherResult.getToken();
                    tdNtTeacherResult.setTea_password(MD5.code(tdNtTeacherRetrive.getTea_password()));
                    tdNtTeacherResult.setToken(CommonUtil.getToken());
                    tdNtTeacherDao.updateTdNtTeacherPsdAndToken(tdNtTeacherResult);
                    MemcachedUtils.delete(token);
                    retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                    retInfo.setTip(ValidationMessages.getValue("TEACHER.RETRIEVE.SUCCESS"));

                } else {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("TEACHER.INVITE.CODE.ERROR"));
                }
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
